package com.bluegalaxy.sitescore.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bluegalaxy.sitescore.common.enums.RecordStatus;
import com.bluegalaxy.sitescore.entity.HatCity;
import com.bluegalaxy.sitescore.entity.TbSitePicture;
import com.bluegalaxy.sitescore.mapper.HatCityMapper;
import com.bluegalaxy.sitescore.service.support.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bluegalaxy.sitescore.mapper.TbSiteInfoMapper;
import com.bluegalaxy.sitescore.entity.TbSiteInfo;
import com.bluegalaxy.sitescore.service.ITbSiteInfoService;

import java.math.BigDecimal;
import java.util.List;
import java.util.regex.Pattern;

/**
 *
 * TbSiteInfo 表数据服务层接口实现类
 *
 */
@Service
public class TbSiteInfoServiceImpl extends BaseServiceImpl<TbSiteInfoMapper, TbSiteInfo> implements ITbSiteInfoService {

    @Autowired
    private TbSiteInfoMapper siteInfoMapper;

    @Autowired
    private HatCityMapper hatCityMapper;

    public Page<TbSiteInfo> getSiteInfoList(Page<TbSiteInfo> page,String province,String city)
    {
        EntityWrapper<TbSiteInfo> ew = new EntityWrapper<TbSiteInfo>(new TbSiteInfo());
        TbSiteInfo tbSiteInfo=new TbSiteInfo();
       // tbSiteInfo.setStatus(RecordStatus.Effective.getValue().shortValue());
       if(StringUtils.isEmpty(province)&&StringUtils.isEmpty(city)) {
          /* ew.setEntity(tbSiteInfo);
           ew.where("status!=0");
           ew.orderBy("id desc");
              page.setRecords(siteInfoMapper.selectPage(page,ew));*/
          return page;
       }
       else {
           tbSiteInfo.setProvince(province);
           tbSiteInfo.setCity(city);
           ew.setEntity(tbSiteInfo);
           ew.where("status!=0");
           ew.orderBy("field(status,-1,1,2,3),create_time DESC");
           page.setRecords(siteInfoMapper.selectPage(page,ew));
       }
        return page;
    }

    @Override
    public Page<TbSiteInfo> getSiteInfoListByIdAndName(Page<TbSiteInfo> page, String province, String city, String id, String name) {
        EntityWrapper<TbSiteInfo> ew = new EntityWrapper<TbSiteInfo>(new TbSiteInfo());
        TbSiteInfo tbSiteInfo=new TbSiteInfo();
       // tbSiteInfo.setStatus(RecordStatus.Effective.getValue().shortValue());
        if(StringUtils.isEmpty(province)&&StringUtils.isEmpty(city)) {
            /*ew.setEntity(tbSiteInfo);
            ew.where("status!=0");
            ew.orderBy("id desc");
            page.setRecords(siteInfoMapper.selectPage(page,ew));*/
            return  page;
        }
        else {
            Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
            tbSiteInfo.setProvince(province);
            tbSiteInfo.setCity(city);
            if(!StringUtils.isEmpty(id)&&pattern.matcher(id).matches()) {
                tbSiteInfo.setId(Integer.parseInt(id));
            }
            if(!StringUtils.isEmpty(name)) {
                tbSiteInfo.setSiteName(name);
            }
            ew.setEntity(tbSiteInfo);
            ew.where("status!=0");
            ew.orderBy("id desc");
            page.setRecords(siteInfoMapper.selectPage(page,ew));
        }
        return page;
    }

    @Override
    public Page<TbSiteInfo> getSiteInfoByIdAndName(Page<TbSiteInfo> page, String id, String name) {
        EntityWrapper<TbSiteInfo> ew = new EntityWrapper<TbSiteInfo>(new TbSiteInfo());
        TbSiteInfo tbSiteInfo=new TbSiteInfo();
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        if(!StringUtils.isEmpty(id)&&pattern.matcher(id).matches()) {
            tbSiteInfo.setId(Integer.parseInt(id));
        }
        if(!StringUtils.isEmpty(name)) {
            tbSiteInfo.setSiteName(name);
        }
        ew.setEntity(tbSiteInfo);
        ew.where("status in (2,3,-1)");
        ew.orderBy("field(status,2,3,-1),audit_time DESC");
        page.setRecords(siteInfoMapper.selectPage(page,ew));
        return  page;
    }

    @Override
    public Page<TbSiteInfo> getSiteInfoListAll(Page<TbSiteInfo> page) {
        return siteInfoMapper.selectSiteInfoListAll(page);
    }

    @Override
    public Page<TbSiteInfo> getSiteInfoExamine(Page<TbSiteInfo> page) {
        EntityWrapper<TbSiteInfo> ew = new EntityWrapper<TbSiteInfo>(new TbSiteInfo());
        ew.where("status in (2,3,-1)");
        ew.orderBy("field(status,2,3,-1),audit_time DESC");
        page.setRecords(siteInfoMapper.selectPage(page,ew));
        return page;
    }

    @Override
    public TbSiteInfo getSiteInfoById(Long siteId)
    {
        return siteInfoMapper.selectById(siteId);
    }

    @Override
    public List<TbSiteInfo> getSiteInfoAll() {
        return siteInfoMapper.selectSiteInfoAll();
    }

    @Override
    public void deleteSiteInfoById(Long siteId) {
        TbSiteInfo tbSiteInfo=siteInfoMapper.selectById(siteId);
        tbSiteInfo.setStatus(RecordStatus.Invalid.getValue().shortValue());
        siteInfoMapper.updateSelectiveById(tbSiteInfo);
    }

    @Override
    public void updateSiteInfoExamine(Long siteId) {
        TbSiteInfo tbSiteInfo=siteInfoMapper.selectById(siteId);
        tbSiteInfo.setStatus(RecordStatus.Approved.getValue().shortValue());
        siteInfoMapper.updateSelectiveById(tbSiteInfo);
    }

    @Override
    public boolean updateSiteInfo(TbSiteInfo tbSiteInfo) {
        return siteInfoMapper.updateById(tbSiteInfo)==1;
    }

    @Override
    public boolean updateSiteInfoScore(int siteId, BigDecimal score) {
        TbSiteInfo tbSiteInfo = getSiteInfoById(Long.valueOf(siteId));
        tbSiteInfo.setScore(score.floatValue());
        if(updateSiteInfo(tbSiteInfo)){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean examineSiteInfoById(int id, String idea, boolean flag) {
        TbSiteInfo siteInfo = new TbSiteInfo();
        siteInfo.setId(id);
        if (siteInfoMapper.selectCount(siteInfo)==0){
            return false;
        }
        siteInfo = siteInfoMapper.selectOne(siteInfo);
        siteInfo.setIdea(idea);
        if (flag == true){
            siteInfo.setStatus(RecordStatus.Audit.getValue().shortValue());
        }else {
            siteInfo.setStatus(RecordStatus.Reject.getValue().shortValue());
        }
        siteInfoMapper.updateById(siteInfo);
        return true;
    }

}